import * as feishuService from '@/api/service/FeishuService'



/**
 * 表格列表
 */
export function findList(queryBody?:any): Promise<any> {
  const tableId = globalThis.tableConfig.account

  return feishuService.appTableRecords(tableId, queryBody, 100)
}

/**
 * 按账号查询
 * @param accountId
 * @returns
 */
export async function findByAcountId(accountId: string): Promise<any> {
  const queryBody:any = {
    filter: {
      conjunction:'and',
      conditions: [
        {
          field_name:'账号ID',
          operator:'is',
          value:[accountId],
        }
      ]
    }
  }
  const tableId = globalThis.tableConfig.account
  const response = await feishuService.appTableRecordList(tableId, queryBody);
  const items = response.data.items;
  return items.length > 0 ? items[0] : null;
}

/**
 * 新增记录
 * @param record
 */
export function addRecord(record:any):Promise<any>{
  const tableId = globalThis.tableConfig.account
  const fields = {
    "账号ID": record['账号ID'],
    "更新日期": new Date().getTime(),
    "偏好设置": record['偏好设置']
  }
  return feishuService.addRecord(tableId, fields)
}

/**
 * 更新记录
 * @param record
 */
export function updateRecord(recordId: string, record: any):Promise<any>{
  const tableId = globalThis.tableConfig.account
  const fields = {
    "账号ID": record['账号ID'],
    "更新日期": new Date().getTime(),
    "偏好设置": record['偏好设置']
  }

  return feishuService.updateRecord(tableId, recordId, fields)
}


/**
 * 删除记录
 * @param recordId
 */
export function removeRecord(recordId:string):Promise<any>{
  const tableId = globalThis.tableConfig.account
  return feishuService.removeRecord(tableId, recordId);
}
